Cust | Bike | Rent_Start | Rent_End |
---|---|---|---|
1 | 1 | 2025-04-15 10:00:00 | 2025-04-20 18:00:00 |
2 | 2 | 2025-04-01 11:00:00 | 2025-04-02 17:00:00 |
3 | 3 | 2025-04-02 13:00:00 |
??? |
Cust | Bike | Rent_Start | Rent_End |
---|---|---|---|
1 | 1 | 2025-04-15 10:00:00 | infinity |
2 | 2 | 2025-04-01 11:00:00 | 2025-04-02 17:00:00 |
3 | 3 | 2025-04-02 13:00:00 |
infinity |
Cust | Bike | Rent_Start | Rent_End |
---|---|---|---|
2 | 2 | 2025-04-02 11:00:00 | 2025-04-01 17:00:00 |
Cust | Bike | Rent_Start | Rent_End |
---|---|---|---|
1 | 1 | 2025-04-01 10:00:00 | 2025-04-04 18:00:00 |
3 | 1 | 2025-04-02 13:00:00 |
2025-04-02 16:00:00 |
Cust | Bike | Rent_Start | Rent_End |
---|---|---|---|
1 | 1 | 2025-04-01 10:00:00 | 2025-04-04 18:00:00 |
3 | 1 | 2025-04-02 13:00:00 |
2025-04-02 16:00:00 |
Cust | Bike | Rent_Start | Rent_End |
---|---|---|---|
1 | 1 | 2025-04-01 10:00:00 | 2025-04-04 18:00:00 |
3 | 1 | 2025-04-02 13:00:00 |
2025-04-02 16:00:00 |
Cust | Bike | Rent_Range |
---|---|---|
1 | 1 | [2025-04-01 10:00:00, 2025-04-04 18:00:00) |
3 | 3 | [2025-04-02 13:00:00, 2025-04-02 16:00:00) |
Cust | Bike | Rent_Range |
---|---|---|
1 | 1 | [2025-04-01 10:00:00, 2025-04-04 18:00:00) |
3 | 3 | [2025-04-02 13:00:00, infinity) |
'(10,50)'::int4range
'[15.99, 29.99]'::numrange
'[2025-04-01,2025-04-05)'::daterange
'{[1,5), [10,15)}'::int4range[];
Index Type | Operators |
---|---|
Btree, Hash | =< |
GiST | << &< &> >> @> <@ && |
SP-GiST | << >> @> <@ |
create table Rental (
Bike_Id integer,
Customer_Id integer,
Rental_Range tstzrange,
/* Constraints */
primary key (Bike_Id, Customer_Id),
exclude using gist (Bike_Id with =, Rental_Range with &&)
);
laetitia=# create table test(
id integer primary key,
value text);
CREATE TABLE
laetitia=# create sequence my_seq;
CREATE SEQUENCE
laetitia=# insert into test (
select nextval('my_seq'),
'blabla');
INSERT 0 1
laetitia=# create sequence my_seq;
CREATE SEQUENCE
laetitia=# create table test (
id integer default nextval('my_seq') primary key,
value text);
CREATE TABLE
laetitia=# insert into test(value) values ('blabla');
INSERT 0 1
laetitia=# create table test (
id serial primary key,
value text);
CREATE TABLE
laetitia=# insert into test (value) values ('blabla');
INSERT 0 1
laetitia=# create table test (
id integer generated always as identity primary key,
value text);
CREATE TABLE
laetitia=# insert into test (value) values ('blabla');
INSERT 0 1
Prevents from inserting or updating manually this column
laetitia=# insert into test (id, value) values (2,'blabla');
ERROR: cannot insert a non-DEFAULT value into column "id"
DETAIL: Column "id" is an identity column defined
as GENERATED ALWAYS.
HINT: Use OVERRIDING SYSTEM VALUE to override.
Sequence |
Serial | Identity column | |
Automatic value | ❌ | ✅ |
✅ |
Not null constraint |
❌ | ✅ | ✅ |
Prevent manual insert | ❌ | ❌ |
✅(with 'always') |
How much time was that bike rented to that customer?
select
ceil(
extract(
epoch from (
upper(Rental_Range) - lower(Rental_Range)
)
) / (12 * 3600)
)
from Rental
where Bike_Id = X
and Customer_Id = Y;
How much time was that bike rented to that customer?
create table Rental (
Bike_Id integer,
Customer_Id integer,
Rental_Range tstzrange,
/* Generated column */
Rental_duration integer always generated as (
ceil(extract(epoch from (upper(Rent_Range) - lower(Rent_Range))) / (12 * 3600)) * 0.5
) stored,
/* Constraints */
primary key (Bike_Id, Customer_Id),
exclude using gist (Bike_Id with =, Rental_Range with &&)
);
select Rental_Duration
from Rental
where Bike_Id = X
and Customer_Id = Y;
select price(Rental_Duration)
from Rental
where Bike_Id = X
and Customer_Id = Y;
create table Rental (
Bike_Id integer,
Customer_Id integer,
Rental_Range tstzrange,
/* Generated column */
Rental_duration integer always generated as (
ceil( extract(epoch from upper(Rent_Range)) - extract(epoch from lower(Rent_Range))) / (12 * 3600)) * 0.5
) stored,
/* Columns with trigger */
Price numeric(6,2),
/* Constraints */
primary key (Bike_Id, Customer_Id),
exclude using gist (Bike_Id with =, Rental_Range with &&)
);
create or replace function set_price()
returns triggeR
language plpgsql
as
$set_price$
begin
if new.price is null then
new.price=price(new.Rental_Range);
end if;
return new;
end;
$set_price$;
create trigger Rental_before_insert
before insert
on Rental
for each row
execute function set_price();
create table bike (
bike_Id integer generated always by identity,
/* Enum columns */
category bike_category,
size bike_size,
/* other search criteria*/
description text,
primary key (Bike_Id)
)
create index Bike_Description on
Bike (description text_pattern_ops);
select Bike_Id, description
from bike
where description ~* '21[- ]speed|full suspension';
create table bike (
bike_Id integer generated always by identity,
/* Enum columns */
category bike_category,
size bike_size,
/* other search criteria*/
description tsvector,
primary key (Bike_Id)
)
create index Bike_description_tsv on
bikes using gin (description);
select Bike_Id, description
from bike
where description_tsv @@
to_tsquery('english', 'electric & "21-speed"');
create or replace function notify_repair_needed()
returns trigger as
$notify_repair_needed$
begin
if new.needs_repair and not old.needs_repair then
notify repairs, new.Bike_Id;
end if;
return new;
end;
$notify_repair_needed$ language plpgsql;
def listen_for_notifications(self):
self.cursor.execute("listen repairs;")
print("Listening for repair notifications...")
while True:
# Check for notifications
self.conn.poll()
while self.conn.notifies:
notify = self.conn.notifies.pop()
bike_id = notify.payload
print(f"Received repair notification for bike {bike_id}")
self.bikes_needing_repair.add(bike_id)
insert into rental (
Bike_Id,
customer_Id,
Rent_Range)
select 1, 1, tstzrange(now(), 'infinity')
returning price;
);
with myBike (id) as (insert into Bike (
Category,
Size,
Description)
select 'road',
'M',
$$The new Carbon All-Road v2 is built for bigger adventures
...
bike for the latest drivetrain tech and easier maintenance.$$
returning Bike_Id
)
insert into Rental (
Bike_Id,
Customer_Id,
Rent_Range)
select id, 1, tstzrange(now(), 'infinity')
from myBike
returning price
;